<?php
class Nilai_raport_persiswa extends CI_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->load->helper('flexigrid');
		$this->load->helper('format');
		$this->load->library('flexigrid');
		$this->load->library('form_validation');
		$this->load->model('master_model');
		$this->load->model('pelajaran_model');
		$this->load->model('penilaian_model');
		$this->load->model('siswa_model');
	}
	
	function index($id_sekolah, $id_ta, $id_tingkat, $id_kelas, $id_semester, $act='')
	{
		
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		// $nilai_raport = $this->penilaian_model->getNilairaport($id_sekolah, $id_ta, $id_tingkat, $id_kelas, $id_semester, $kode_mp);
		// $jml_kolom=$this->penilaian_model->getUjian($id_sekolah, $id_ta, $kode_mp);
		$colModel['no'] = array('No',40,TRUE,'center',0);
		$colModel['NISN'] = array('NISN',100,TRUE,'center',1);
		$colModel['NAMA_LENGKAP'] = array('Nama Lengkap',300,TRUE,'center',1);
		$colModel['ALAMAT'] = array('Alamat',300,FALSE,'center',0);
		$colModel['print'] = array('Cetak',100,FALSE,'center',0);
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => '335',
		'rp' => 15,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Displaying: {from} to {to} of {total} items.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Siswa >> Lihat Raport'
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		
		$buttons[] = array('Lihat Detail Nilai Raport','view','org_js');
		$buttons[] = array('separator');
		$buttons[] = array('Back to Pelajaran Guru','back','org_js');
		
		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		$grid_js = build_grid_js('flex1',site_url("/nilai_raport_persiswa/listraportpersiswa_grid/".$this->session->userdata('unitid')."/".$id_ta."/".$id_tingkat."/".$id_kelas."/".$id_semester),$colModel,'no','asc',$gridParams,$buttons);
		
		if($this->session->flashdata('suc_msg'))
			$data['header_info'] = '<div class="ret_success">'.$this->session->flashdata('suc_msg').'</div>';
			
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"<script type='text/javascript'>
			function org_js(com,grid)
			{
				
				if (com=='Back to Pelajaran Guru')
				{
					location.href='".base_url()."index.php/nilai_siswa_raport/'; 
				}
				
				if (com=='Lihat Detail Nilai Raport')
				{
				   if($('.trSelected',grid).length==1){
						var items = $('.trSelected',grid);
						var itemlist =items[0].id.substr(3);
						
						popup('".base_url()."index.php/nilai_raport_persiswa/lihat_raport/".$id_tingkat."/".$id_kelas."/".$id_semester."/'+itemlist,1050,400);
						// popup('".base_url()."index.php/nilai_raport_persiswa/lihat_raport2/',50,600);
						
						
					} else {
						alert('Silakan Pilih Satu Siswa, Lalu klik tombol Lihat Detail Nilai Raport');
						return false;
					} 
				}     
				   
			} 
			
			function popup(url,width,height) 
			{
				 var left   = (screen.width  - width)/2;
				 var top    = (screen.height - height)/2;
				 var params = 'width='+width+', height='+height;
				 params += ', top='+top+', left='+left;
				 params += ', directories=no';
				 params += ', location=no';
				 params += ', menubar=no';
				 params += ', resizable=no';
				 params += ', scrollbars=yes';
				 params += ', status=no';
				 params += ', toolbar=no';
				 newwin=window.open(url,'windowname5', params);
				 if (window.focus) {newwin.focus()}
				 return false;
			}
			</script>
			";
			
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
	}
	
	function listraportpersiswa_grid($id_sekolah,$id_ta,$id_tingkat,$id_kelas,$id_smt)
	{	
	
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		$valid_fields = array('NAMA_TA','NISN','NAMA_LENGKAP','KET');
		
		$this->flexigrid->validate_post('a.NISN','asc',$valid_fields);

		$records = $this->penilaian_model->get_siswa_kelas($id_sekolah,$id_ta,$id_tingkat,$id_kelas);
		
		$this->output->set_header($this->config->item('json_header'));
		$no = 0;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		
		foreach ($records['records']->result() as $row)
		{
			$record_items[] = array($row->NISN,
				$no+1,
				'<div align="left">'.$row->NISN.'</div>',
				'<div align="left">'.$row->NAMA_LENGKAP.'</div>',
				'<div align="left">'.$row->ALAMAT_ASAL.'</div>',
				'<a href=\''.site_url().'/nilai_raport_persiswa/cetak_raport/'.$id_tingkat.'/'.$id_kelas.'/'.$id_smt.'/'.$row->NISN.'/pdf/\'><img border=\'0\' src=\''.base_url().'images/report/pdf.gif\'></a> &nbsp;&nbsp;
				<a href=\''.site_url().'/nilai_raport_persiswa/cetak_raport/'.$id_tingkat.'/'.$id_kelas.'/'.$id_smt.'/'.$row->NISN.'/xls/\'><img border=\'0\' src=\''.base_url().'images/report/xls.png\'></a> '
				);
				$no++;
		}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	
	function lihat_raport($id_tingkat, $id_kelas, $id_semester, $nisn){
		$data['type'] = 'html';
		$data['nisn'] = $nisn;
		$data['nama'] = $this->siswa_model->get_siswaID($nisn)->row()->NAMA_LENGKAP;
		$data['tahunajaran'] = $this->pelajaran_model->getTa($this->session->userdata('unitid'))->row()->TAHUN_AJARAN;
		$data['r_tingkat'] = $this->pelajaran_model->getTingkat($this->session->userdata('unitid'),$id_tingkat)->row()->TINGKAT;
		$data['r_kelas'] = $this->pelajaran_model->getKelas($this->session->userdata('unitid'),$id_kelas)->row()->KELAS;
		$data['r_semester'] = $this->pelajaran_model->getSmt($this->session->userdata('unitid'),$id_semester)->row()->SEMESTER;
		$data['r_nilairaport'] = $this->penilaian_model->getRaportSiswa($this->session->userdata('unitid'), $this->session->userdata('tahunajaran'), $id_tingkat, $id_kelas, $id_semester,$nisn);
		// $data['grading'] = $this->penilaian_model->getGrading($this->session->userdata('unitid'), $this->session->userdata('tahunajaran'));
		$this->load->view('form_nilai_raport_persiswa',$data);
	}
	// function lihat_raport2(){
		// $data['content'] = $this->load->view('form_nilai_raport_persiswa');
	// }
	
	function cetak_raport($id_tingkat, $id_kelas, $id_semester, $nisn, $type){
		$this->load->helper('to_pdf');
		$this->load->helper('file');
		
		$data['nisn'] = $nisn;
		$data['nama'] = $this->siswa_model->get_siswaID($nisn)->row()->NAMA_LENGKAP;
		$data['tahunajaran'] = $this->pelajaran_model->getTa($this->session->userdata('unitid'))->row()->TAHUN_AJARAN;
		$data['r_tingkat'] = $this->pelajaran_model->getTingkat($this->session->userdata('unitid'),$id_tingkat)->row()->TINGKAT;
		$data['r_kelas'] = $this->pelajaran_model->getKelas($this->session->userdata('unitid'),$id_kelas)->row()->KELAS;
		$data['r_semester'] = $this->pelajaran_model->getSmt($this->session->userdata('unitid'),$id_semester)->row()->SEMESTER;
		$data['r_nilairaport'] = $this->penilaian_model->getRaportSiswa($this->session->userdata('unitid'), $this->session->userdata('tahunajaran'), $id_tingkat, $id_kelas, $id_semester,$nisn);
		if($type == 'pdf'){
			$filename="Raport_";
			$data['type'] = 'pdf';
		
			$html = $this->load->view('form_nilai_raport_persiswa',$data,TRUE);
			pdf_create($html, $filename);
		}
		else if($type == 'xls'){
			$data['filename']="Raport_xls";
			$data['type'] = 'xls';
			$this->load->view('form_nilai_raport_persiswa',$data);
		}
		
		// $log['activity']='PRINT LAPORAN';
		// $log['information']="PRINT Laporan Daftar Produk ";
		// $this->model_log->add($log);
		// redirect('sys/report/report_product');
	
	}	
	
}
?>
